import { createRouter, createWebHistory, createWebHashHistory } from 'vue-router'
import type {RouteRecordRaw} from 'vue-router'
import HomeView from "@/views/HomeView.vue";
import  userRouter from './user'
import accountRouter from './account'
import goodRoute from './good'

const routes: Array<RouteRecordRaw> = [
  {
    path: "/",
    redirect: '/home'
  },
  {
    path: "/home",
    name: "home",
    component: HomeView,
  },
  {
    path: "/search",
    name: "search",
    component: () => import("@/views/search.vue")
  },
  {
    path: '/login',
    name: "login",
    component: () => import("@/views/sign/login.vue"),
    meta: {
      requiresAuth: false
    }
  },
  {
    path: "/about",
    name: "about",
    component: () => import("@/views/AboutView.vue"),
  },
  {
    path: "/category",
    name: "category",
    component: () => import("@/views/category.vue"),
  },
  {
    path: "/my",
    name: "my",
    component: () => import("@/views/my.vue"),
    meta: {
      requiresAuth: true
    }
  },
  {
    path: "/certification",
    name: "certification",
    component:()=>import("@/views/sign/certification.vue"),
    meta: {
      requiresAuth: true
    }
  },
  {
    path: '/good/:id',
    name: 'good',
    component: () => import("@/views/good/detail.vue"),
    meta: {
      requiresAuth: false
    }
  },
  {
    path: '/list',
    name: 'list',
    component: () => import("@/views/good/list.vue"),
    meta: {
      requiresAuth: false
    }
  }
].concat(userRouter).concat(accountRouter).concat(goodRoute);
const router = createRouter({
  history: createWebHashHistory(import.meta.env.BASE_URL),
  routes
});

router.beforeEach((to: any, from: any,  next: any) => {
  if (!localStorage.getItem('uid') && to.meta.requiresAuth) {
    next({ path: '/login' })
    return;
  } else {
    next()
  }
  // else next()
})

export default router;
